import React, { Component } from 'react';
import { withRouter, NavLink } from 'react-router-dom';
import { Menu, Divider, Avatar } from 'antd';
import headimgurl from './../../statics/icon/headimg.svg';
import store from './../../store';
import './index.less';
class Aside extends Component {
	state = {
		headimgurl: sessionStorage.getItem("headimgurl"),
	}
	
	componentWillUnmount(){
		this.unsubscribe();
	}
	
	componentDidMount(){
		this.unsubscribe = store.subscribe(this.handleStoreChange);
	}
	
	handleStoreChange = () => {
		this.setState({headimgurl: sessionStorage.getItem("headimgurl")+"?"+Date.now()})
	}
	
	renderMenu = (data) => {
		return data.map((item, index) => {
			return 	<Menu.Item key={item.unique_code} className="back-menu-item">
						<NavLink to={item.unique_code}>
							<svg
								className="icon back-nav-icon"
								aria-hidden="true"
							>
								<use xlinkHref={item.icon}></use>
							</svg>&nbsp;&nbsp;
							{item.name}
							&nbsp;&nbsp;
						</NavLink>
					</Menu.Item>
		})
	}
	UNSAFE_componentWillMount(){
		const menuTreeNode = this.renderMenu(this.props.menuList);
		this.setState({
			menuTreeNode
		})
	}
	
	//头像加载失败
	logoImgError = () => {
		this.setState({
			headimgurl
		})
	}
	
	render(){
		const path = this.props.location.pathname;
		const menuType = this.props.menuType;
		return (
			<div className="navleft-wrap">
				<Menu
					mode="inline"
					defaultSelectedKeys={['1']}
					defaultOpenKeys={['sub1']}
					selectedKeys={path}
					style={{ height: '100%', borderRight: 0 }}
				>
					<div className="userinfo-wrap">
						<div className="user-box">
							<NavLink to="/setting/personal/info">
								<Avatar size={56} src={this.state.headimgurl+"?"+Date.now()} onError={this.logoImgError} />
							</NavLink>
							{/*// <div>
							// 	<p>您好:</p>
							// 	<p>{userName}</p>
							// </div>*/}
						</div>
						<Divider>
							{menuType === "personal"?"个人中心":"后台设置"}
						</Divider>
					</div>
					{ this.state.menuTreeNode }
				</Menu>
			</div>
		)
	}
}

export default withRouter(Aside);